//
//  NoticeView.m
//  meiyinxiang
//
//  Created by apple on 25/10/2017.
//  Copyright © 2017 hbjApple. All rights reserved.
//

#import "NoticeView.h"
#import <HexColor.h>
@interface NoticeView()
{
    UILabel *label ;
    UIScrollView *showView;
    dispatch_source_t timer;
    CGSize size;
    CGFloat scrollViewWith;
    CGFloat timeInterval;
}
@end
@implementation NoticeView
+ (id)loadNibWithNoticeView{
    UINib *nib = [UINib nibWithNibName:@"NoticeView" bundle:nil];
    return [[nib instantiateWithOwner:nil options:nil] lastObject];
}
- (void)awakeFromNib{
    [super awakeFromNib];
   // [self addRollAction];
}
#pragma mark - RowAction
- (void)addRollAction{
    // 注册字体
    
    // 获取文本
//    NSString *string = @"点击我戳红包！！,我是一个大傻比，草丛丛哦，实际上就是科举考试就考试，就是看精神科就是苦上加苦，可是就是苦上加苦手机上看精神科色即是空就是苦上加苦是看精神科九分裤大家疯狂点击付款即可";
    [self addViews];
    [self rollAnimation];
    
}
- (void)addViews{
    // 初始化label
    if (label == nil) {
        label      = [UILabel new];
    }
    label.text          = _noticeString.length > 0 ? _noticeString : @"天降红包，戳我抢红包~~";
    label.numberOfLines = 0;
    label.textColor     = [UIColor colorWithHexString:@"333333"];
    label.font          = [UIFont fontWithName:@"PingFang-SC-Light" size:14.];
    
    // 计算尺寸
    CGSize size = [label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName,nil]];
    // CGSize size = [label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName,nil]];
    ;
    label.frame  = (CGRect){CGPointZero, size};
    
    // 初始化ScrollView
    CGFloat scrollViewWith = ScreenWidth - 127;
    timeInterval = label.text.length * 1.0;
    showView =
    [[UIScrollView alloc] initWithFrame:CGRectMake(47, 6, scrollViewWith, size.height)];
    showView.contentSize   = size;
    showView.showsHorizontalScrollIndicator = NO;
    [showView addSubview:label];
    [self addSubview:showView];
    
    // 形成边缘的遮罩
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 6, scrollViewWith, size.height)];
    [self addSubview:lineView];
}
- (void)setNoticeString:(NSString *)noticeString{
    _noticeString = noticeString;
    // 计算尺寸
     size = [label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName,nil]];
     scrollViewWith = ScreenWidth - 127;
     timeInterval = label.text.length * 0.5;

}
- (void)rollAnimation{
    // 动画
 
    //获取队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    //创建定时器
    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //设置定时器的各种属性
    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC));//比当前时间晚开始1秒
    uint64_t interval = (uint64_t)(timeInterval * NSEC_PER_SEC);
    dispatch_source_set_timer(timer, start, interval, 0);
    // 设置回调
    dispatch_source_set_event_handler(timer, ^{
        [UIView animateKeyframesWithDuration:timeInterval
                                       delay:0
                                     options:UIViewKeyframeAnimationOptionAllowUserInteraction
                                  animations:^{
                                      // 计算移动的距离
                                      CGPoint point = showView.contentOffset;
                                      point.x = size.width - scrollViewWith;
                                      showView.contentOffset = point;
                                  }
                                  completion:^(BOOL finished) {
                                      [showView removeFromSuperview];
                                      showView = nil;
                                      [self addViews];
//                                       showView.contentOffset = CGPointMake(47, 6);
//                                      label.frame  = (CGRect){CGPointZero, size};
                                      
                                  }];
    });
    
  //启动定时器
    dispatch_resume(timer);

   
}
- (void)leaveDispatch{
    dispatch_cancel(timer);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
